package com.example.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.example.domain.Lines;
import com.example.domain.TimeLine;
import com.example.service.LinesService;
import com.example.service.TimeLineService;

@Controller
public class TimeLineController {

	@Autowired
	private TimeLineService timeLineService;
	
	@Autowired
	private LinesService linesService;
	
	@RequestMapping(value="/api/timeLine/{id}", method= RequestMethod.GET)
	public TimeLine getById(@PathVariable("id") Integer id){
		System.out.println("id:" + id);
		return timeLineService.findById(id);
	}
	
	@RequestMapping(value="/api/timeLine/save", method=RequestMethod.POST)
	public String saveTimeLine(@RequestParam("id") Integer id,
			@RequestParam("stageName") String stageName,
			@RequestParam("startDate") String startDate,
			@RequestParam("line") String line,
			@RequestParam("direction") String direction,
			@RequestParam("remark") String remark){
		TimeLine  tl = new TimeLine();
		tl.setStageName(stageName);
		tl.setStartDate(startDate);
		tl.setStageid(0);
		tl.setDirection(direction);
		tl.setRemark(remark);
		tl.setLineId(line);
		timeLineService.insert(tl);
		return "redirect:/api/tl/list";
	}
	
	@RequestMapping("/api/tl")
	public String timeLine(Model model){
		List<Lines> linesList = linesService.findAllLines();
		model.addAttribute("lineList", linesList);
		return "metro/tl";
	}
	
	@RequestMapping("/api/tl/list")
	public String getTLList(Model model){
		List<TimeLine> list = timeLineService.findAll();
		model.addAttribute("items", list);
		return "metro/tllist";
	}
}
